package com.spring.common.web.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.google.common.collect.Lists;
import com.oa.api.web.dao.OaMapper;
import com.spring.common.web.dao.CommonSelMapper;
import com.spring.common.web.service.CommonSelService;
import com.spring.security.core.util.ApplicationUtils;

@Service
public class CommonSelServiceImpl implements CommonSelService{
	
	@Resource
	private CommonSelMapper commonSelMapper;
	@Resource
	private OaMapper oaMapper;
	
	@Override
	public List<Map<String, Object>> getParkSel(Map<String, Object> m) {
		return commonSelMapper.getParkSel(m);
	}
	
	/**
	 * 查询OA部门
	 */
	@Override
	public List<Map<String, Object>> queryDepartmentPark(String dept_id) {
		Map<String,Object> sp = new HashMap<String,Object>();
		sp.put("dept_id", dept_id);
//		List<Map<String, Object>> m = oaMapper.queryDepartmentPark2(sp);
//		List<Map<String, Object>> parm = new ArrayList<Map<String, Object>>();
//		for(Map<String, Object> model:m){
//			parm.add(model);
//		}
		List<Map<String, Object>> rdata = Lists.newArrayList();
		if(ApplicationUtils.isNotNull(dept_id)){
			rdata = oaMapper.queryDepartmentParkChildrenAll(sp);
//			getDeptChildrenByPidAll(rdata,parm);
//		}else{
//			getDeptChildrenByPid(rdata,parm);
		}
		return  rdata;
	}

	@Override
	public List<Map<String, Object>> easyTreeDeptUser(String dept_id) {
		Map<String,Object> sp = new HashMap<String,Object>();
		sp.put("dept_id", dept_id);
		List<Map<String, Object>> m = oaMapper.queryDepartmentPark2(sp);
		List<Map<String, Object>> parm = new ArrayList<Map<String, Object>>();
		List<Map<String, Object>> cList = Lists.newArrayList();
		for(Map<String, Object> model:m){
			parm.add(model);
			cList.add(model);
		}
		getDeptUserChildrenByPid(cList,parm);
		return  cList;
	}

	/**
	 * 递归查出部门父节点的用户及子部门节点
	 * @param m
	 */
	private void getDeptUserChildrenByPid(List<Map<String, Object>> cList,List<Map<String, Object>> m){
		Map<String,Object> parm = new HashMap<String,Object>();
		String dept_id = "";
		for(Map<String, Object> model:m){
			dept_id = ApplicationUtils.Object2String(model.get("id"));
			if(ApplicationUtils.isNotNull(dept_id)){
				parm.put("dept_id", dept_id);
				//获取部门人员
				List<Map<String, Object>> users =  oaMapper.queryUserByDeptId(parm);
				for(Map<String, Object> ru:users){
					cList.add(ru);
				}
				//获取下级部门
				List<Map<String, Object>> cm = oaMapper.queryDepartmentParkChildrenAll(parm);
				for(Map<String, Object> rd:cm){
					cList.add(rd);
				}
				if(cm!=null&&cm.size()>0){
					//model.put("children",cList);
					getDeptUserChildrenByPid(cList,cm);
				}
			}
		}
	}

	/**
	 * 查询OA部门
	 */
	@Override
	public List<Map<String, Object>> easyTreeDept(String dept_id) {
		Map<String,Object> sp = new HashMap<String,Object>();
		sp.put("dept_id", dept_id);
		List<Map<String, Object>> m = oaMapper.queryDepartmentPark2(sp);
		List<Map<String, Object>> parm = new ArrayList<Map<String, Object>>();
		for(Map<String, Object> model:m){
			parm.add(model);
		}
//		List<Map<String, Object>> rdata = Lists.newArrayList();
//		if(ApplicationUtils.isNotNull(dept_id)){
//			rdata = oaMapper.queryDepartmentParkChildrenAll(sp);
//			getDeptChildrenByPidAll(rdata,parm);
//		}else{
		getDeptChildrenByPid(parm);
//		}
		return  parm;
	}
	
	/**
	 * 递归查出部门父节点的子部门节点
	 * @param m
	 */
	private void getDeptChildrenByPid(List<Map<String, Object>> m){
			Map<String,Object> parm = new HashMap<String,Object>();
			String dept_id = "";
			for(Map<String, Object> model:m){
				dept_id = ApplicationUtils.Object2String(model.get("id"));
				if(ApplicationUtils.isNotNull(dept_id)){
					parm.put("dept_id", dept_id);
					List<Map<String, Object>> cm = oaMapper.queryDepartmentParkChildrenAll(parm);
					if(cm!=null&&cm.size()>0){
						model.put("children",cm);
						getDeptChildrenByPid(cm);
					}
				}
			}
	}
	
	/**
	 * 递归查出部门父节点的子部门节点
	 */
//	private void getDeptChildrenByPidAll(List<Map<String, Object>> m,List<Map<String, Object>> nparm){
//		if(m!=null&&m.size()>0){
//			Map<String,Object> parm = new HashMap<String,Object>();
//			String dept_id = "";
//			for(Map<String, Object> model:nparm){
//				dept_id = ApplicationUtils.Object2String(model.get("id"));
//				if(ApplicationUtils.isNotNull(dept_id)){
//					parm.put("dept_id", dept_id);
//					List<Map<String, Object>> cm = oaMapper.queryDepartmentParkChildrenAll(parm);
//					if(cm!=null&&cm.size()>0){
//						model.put("items",cm);
//						getDeptChildrenByPid(m,cm);
//					}
//				}
//			}
//		}
//	}

	@Override
	public List<Map<String, Object>> queryUserByDeptId(String dept_id,String shopDeptId,String priv_id) {
		Map<String,Object> sp = new HashMap<String,Object>();
		if(ApplicationUtils.isNotNull(dept_id)){
			sp.put("dept_id", dept_id);
			return oaMapper.queryUserByDeptId(sp);
		}else if(ApplicationUtils.isNotNull(shopDeptId)&&ApplicationUtils.isNotNull(priv_id)){
			sp.put("dept_id", shopDeptId);
			sp.put("dept_name_py", priv_id);
			/**
			 * 查询所有子部门
			 */
			List<Map<String,Object>> rl = oaMapper.queryDepartmentPark2(sp);
			List<Map<String,Object>> retl = new ArrayList<Map<String,Object>>();
			for(Map<String,Object> m:rl){
				retl.add(m);
			}
			queryAllDeptByParentList(retl,rl);
			String deptIDs = "";
			for(Map<String,Object> retm:retl){
				deptIDs+=ApplicationUtils.Object2String(retm.get("id"))+",";
			}
			if(ApplicationUtils.isNotNull(deptIDs)){
				sp.put("dept_id", deptIDs.substring(0,deptIDs.length()-1));
			}
			
			return oaMapper.queryUserByDeptId(sp);
		}else{
			return new ArrayList<Map<String,Object>>();
		}
	}
	
	/**
	 * 递归子部门
	 * @param rl
	 * @param parml
	 */
	private void queryAllDeptByParentList(List<Map<String,Object>> rl,
				List<Map<String,Object>> parml){
		List<Map<String,Object>> parmN = new ArrayList<Map<String,Object>>();
		if(parml!=null&&parml.size()>0){
			Map<String,Object> pm = new HashMap<String,Object>();
			for(Map<String,Object> model:parml){
				pm.put("dept_id", model.get("id"));
				List<Map<String,Object>> nl = oaMapper.queryDepartmentParkChildrenAll(pm);
				for(Map<String,Object> nm:nl){
					parmN.add(nm);
					rl.add(nm);
				}
			}
			queryAllDeptByParentList(rl,parmN);
		}
		
	}


	@Override
	public List<Map<String, Object>> getScheduleType() {
		return commonSelMapper.getScheduleType();
	}


	@Override
	public List<Map<String, Object>> getCourseOutLook(Map<String,Object> m) {
		return commonSelMapper.getCourseOutLook(m);
	}


	@Override
	public List<Map<String, Object>> getClassRoom(Map<String,Object> m) {
		return commonSelMapper.getClassRoom(m);
	}


	@Override
	public List<Map<String, Object>> getKidsParkCourse(Map<String,Object> m) {
		return commonSelMapper.getKidsParkCourse(m);
	}

	@Override
	public List<Map<String, Object>> getReportSel(String type) {
		return commonSelMapper.getReportSel(type);
	}

	@Override
	public List<Map<String, Object>> signProductSel(Map<String, Object> m) {
		return commonSelMapper.signProductSel(m);
	}

	@Override
	public List<Map<String, Object>> getCustomerSel(Map<String, Object> m) {
		return commonSelMapper.getCustomerSel(m);
	}

	@Override
	public List<Map<String, Object>> getContactSel(Map<String, Object> m) {
		return commonSelMapper.getContactSel(m);
	}

}
